var ns;
app = new Vue({
    el: '#app',
    data: {
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        showtime:false,
        ProjectName:'',
        ReportType:0,//0 设备巡检  1 安防巡更  2 环境保洁
        form: {
            startMonth: '', 
            activeDateDesc: '月份',
            ProjectCompanyType: 1,
            ProjectID:0,
            ProjectIDs: [],
        },
        summary: {},
        projectList: [], 
        WBlist:[],  ////维保数据
        XJlist:[],  ////巡检数据
        userList:[],  ////员工工时
        checkModel:[],//巡检数据
        repairModel:[],//维保数据

    },
    methods: {

        get_data: function () {
            var that = this;
            that.getAnalysisData();
        },
        doSelectProject: function (ID,Name) {
            var that = this;
            that.ProjectName=Name;
            that.form.ProjectID=ID;
            that.form.ProjectIDs=[];
            that.form.ProjectIDs.push(ID);
            that.showsearchparam = false;
            that.doSearch();
        },
        //筛选数据
        getSearchParams: function () {
            var that = this;
            var options = {};
            options.action = "gettaskanalysisparams";
            options.startTime,
            options.endTime,
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.projectList = data.projectList;
                    if(that.projectList!=undefined&&that.projectList.length>0){
                        that.doSelectProject(that.projectList[0].ID,that.projectList[0].Name); 
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            },{
                command: 'App/Device/Summary'
            });
        },
        onShowTime:function(IsShow){
            var that=this;
            that.showtime=IsShow;
        },
        doSelectSearchParam: function (type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            // if (type == 2 && that.showsearchmonth) {
            //     setTimeout(function () {
            //         that.initPicker();
            //     }, 100);
            // }
        },
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        doSearch: function () {
            var that = this;
            that.doHideSearch();
            that.getAnalysisData();
        },
        getAnalysisData: function () {
            var that = this;
            //初始化
            that.XJlist=[];
            that.WBlist=[];
            that.userList=[];
            var planType = 1; ////planType 1 设备巡检  3 安防巡更  4 环境保洁
            if (that.ReportType == 1) {
                planType = 3;
            }
            if (that.ReportType == 2) {
                planType = 4;
            }
            var startTime=moment(that.form.startMonth).startOf('month').format('YYYY-MM-DD');
            var endTime=moment(that.form.startMonth).endOf('month').format('YYYY-MM-DD');

            var options = {};
            // options.action = "gettaskanalysisdata"; ////方法名称
            // options.projectIds = JSON.stringify(that.form.ProjectIDs);
            options.startTime = startTime;
            options.endTime = endTime;
            // options.ProjectCompanyType = that.form.ProjectCompanyType; //筛选类型-项目
            options.planType = planType; 
            console.log(JSON.stringify(options));
            if(that.ReportType == 0){
                ns.post(options, function(succeed, data, err) {
                    if (succeed) {
                       that.do_Succeed(data);
                    } 
                    else if (err) {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }, {
                    toast: true,
                    command: 'App/Device/Summary'
                });
            }
            if(that.ReportType == 1){
                    ns.post(options, function(succeed, data, err) {
                        if (succeed) {
                           that.do_Succeed(data);
                        } 
                        else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }, {
                        toast: true,
                        command: 'App/Patrol/Summary'
                    });
            }
            
        },
        do_Succeed:function(data){
            var that=this;
            if (that.ReportType == 0) {
                that.checkModel = that.dealData(data.checkModel);
                that.repairModel = that.dealData(data.repairModel);
                setTimeout(() => {
                    if (that.checkModel.length > 0) {
                        for (var i = 0; i < that.checkModel.length; i++) {
                            that.loadChart("containerxj" + i, i, that.checkModel[i]);
                        }
                    }
                    if (that.repairModel!=undefined&&that.repairModel.length > 0) {
                        for (var i = 0; i < that.repairModel.length; i++) {
                            that.loadChart("containerwb" + i, i, that.repairModel[i]);
                        }
                    }
                }, 500);
            }
            if (that.ReportType == 1) {
                that.checkModel = that.dealData(data.patrolModel);
                setTimeout(() => {
                    if (that.checkModel.length > 0) {
                        for (var i = 0; i < that.checkModel.length; i++) {
                            that.loadChart("containerxj" + i, i, that.checkModel[i]);
                        }
                    }
                }, 500);
            }
            
        },
        //拼接报表数据格式
        dealData:function(data){
            var that = this;
            var Percent=0;
            if(data.totalCount!=0){
                Percent=(data.completeCount)/data.totalCount;
            }else{
                Percent=0;
            }
            var completePercent=Math.trunc(Percent*100);
            var completePercent2=Math.trunc((1-Percent)*100);
            var chartwc =  [{ "name": "完成率", "y": completePercent }, { "name": "完成率", "y": completePercent2 }];
            var itemdata = [];
            if (that.ReportType == 0) {
                itemdata = [{ "name": "完成率", "y": completePercent, "DeviceClont": data.deviceCount,"CompleteCount": data.completeCount, "TaskCount": data.totalCount, "data": chartwc }];
            }
            else if(that.ReportType == 1) {
                itemdata = [{ "name": "完成率", "y": completePercent, "DeviceClont":0, "CompleteCount": data.completeCount,"TaskCount": data.totalCount, "data": chartwc }];
            }
            return itemdata;
        },
        loadChart: function (IdName,index,item) {
            var processcolor=['#289fff', '#eee'];
            // if(index%2>0){
            //     processcolor=['#ff0000', '#eee'];
            // }
            var chart = new Highcharts.Chart({
                chart: {
                    renderTo: IdName,
                    spacing: [0, 0, 0, 0],
                    panning: false,
                },
                colors: processcolor,
                credits: {
                    enabled: false //不显示Highcharts
                },
                navigation: {
                    buttonOptions: {
                        enabled: false
                    }
                },
                title: {
                    floating: true,
                    text:item.y+'%',
                },
                tooltip: {
                     enabled:false,
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: false,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: false,
                        },
                        // point: {
                        //     events: {
                        //         click: function(e) { // 同样的可以在点击事件里处理
                        //             chart.setTitle({
                        //                 text:e.point.y + ' %'
                        //             });
                        //         }
                        //     }
                        // },
                    }
                },
                series: [{
                    type: 'pie',
                    innerSize: '80%',
                    data: item.data
                }]
            }, function (c) { // 图表初始化完毕后的会掉函数
                // 环形图圆心
                var centerY = c.series[0].center[1],
                    titleHeight = parseInt(c.title.styles.fontSize);
                // 动态设置标题位置
                c.setTitle({
                    y: centerY + titleHeight / 2
                });
            });
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    //月份
                    var btn = document.querySelector('#datemonth');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.startMonth = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.startMonth = rs.value;
                                    _this.doSearch();
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                });
            })(mui, document);
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    var nowDate = new Date();
    var Y = nowDate.getFullYear();
    var M = nowDate.getMonth() + 1 < 10 ? '0' + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
    app.form.startMonth = Y+'-'+M;//初始化默认当前月
    app.ReportType=api.pageParam.servicetype || 0;
    app.initPicker();
    app.get_data();

};
